//
//  AppDelegate.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/27.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AppDelegate.h"
#import "AYSCategoryViewController.h"
#import "AYSHomePageViewController.h"
#import "AYSLiveViewController.h"
#import "AYSSpecialViewController.h"
#import "AYSUserViewController.h"
#import "AYSLeftViewController.h"
#import "AYSSearchViewController.h"


#import "AYSAboutUsViewController.h"
#import "AYSChangeSkinViewController.h"
#import "AYSCollectAllViewController.h"
#import "AYSDisclaimerViewController.h"
#import "AYSSetLanguageViewController.h"
#import "AYSWatchRecordViewController.h"

#import "UMSocial.h"
#import "AYSGuideView.h"
#import "XGPush.h"
#import "AYSGuideViewController.h"
@interface AppDelegate ()<UIScrollViewDelegate>

@property(nonatomic,strong) AYSCategoryViewController *categoryVC;
@property(nonatomic,strong) AYSHomePageViewController *homeVC;
@property(nonatomic,strong) AYSSearchViewController *searchVC;
@property(nonatomic,strong) AYSSpecialViewController *specialVC;
@property(nonatomic,strong) AYSUserViewController *userVC;
@property (nonatomic, strong) AYSLeftViewController *leftVC;

@property (nonatomic,assign) BOOL isHave;

@property (nonatomic,strong) AYSGuideView *guideView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (nonatomic,assign) BOOL isOut;
@property (nonatomic,strong) AYSGuideViewController *guideVC;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [UMSocialData setAppKey:@"561b7a19e0f55a91c80015f2"];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    _homeVC = [[AYSHomePageViewController alloc] init];
    UINavigationController *homeNaVC = [[UINavigationController alloc] initWithRootViewController:_homeVC];
    homeNaVC.tabBarItem.title = @"首页";
    homeNaVC.tabBarItem.image = [UIImage imageNamed:@"homepage"];
    
    _categoryVC = [[AYSCategoryViewController alloc] init];
    UINavigationController *categoryNaVC = [[UINavigationController alloc] initWithRootViewController:_categoryVC];
    categoryNaVC.tabBarItem.title = @"分类";
    categoryNaVC.tabBarItem.image = [UIImage imageNamed:@"category"];
    
    _specialVC = [[AYSSpecialViewController alloc] init];
    UINavigationController *specialNaVC = [[UINavigationController alloc] initWithRootViewController:_specialVC];
    specialNaVC.tabBarItem.title = @"专题";
    specialNaVC.tabBarItem.image = [UIImage imageNamed:@"special"];
    
    _searchVC = [[AYSSearchViewController alloc] init];
    UINavigationController *searchNaVC = [[UINavigationController alloc] initWithRootViewController:_searchVC];
    searchNaVC.tabBarItem.title = @"搜索";
    searchNaVC.tabBarItem.image = [UIImage imageNamed:@"搜索"];
    
    _userVC = [[AYSUserViewController alloc] init];
    UINavigationController *userlNaVC = [[UINavigationController alloc] initWithRootViewController:_userVC];
    userlNaVC.tabBarItem.title = @"趣图";
    userlNaVC.tabBarItem.image = [UIImage imageNamed:@"趣图1"];
    
    _rootVC = [[AYSRootViewController alloc] init];
    _rootVC.tabBar.backgroundImage = [UIImage imageNamed:@"buttom"];
    _rootVC.viewControllers = @[homeNaVC,categoryNaVC,specialNaVC,searchNaVC,userlNaVC];
    _rootVC.tabBar.backgroundColor = [UIColor colorWithWhite:0.075 alpha:1.000];
    _rootVC.tabBar.tintColor = [UIColor redColor];
    
    _leftVC = [[AYSLeftViewController alloc] init];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushVC:) name:@"pushVC" object:nil];
    
    _sideViewController = [[AYSSideViewController alloc] init];
    _sideViewController.rootViewController = _rootVC;
    _sideViewController.leftViewController = _leftVC;
    _sideViewController.leftViewShowWidth = SCREEN_WIDTH-75;
    _sideViewController.needSwipeShowMenu = true;
    _sideViewController.view.backgroundColor = [UIColor grayColor];
    
   
    // 判断是否滑完
    self.isOut = NO;
    
    //打开沙盒，判断沙盒有没有这个文件
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES) lastObject];
    NSString *file = [path stringByAppendingString:@"aa.txt"];
    
    BOOL isHaveFile = [manager fileExistsAtPath:file];
    
    // 如果有，表示曾经进入过主页
    if (isHaveFile)
    {
        // 直接进入主页
        [self goToMainView];
    }
    else
    {
        _guideVC = [[AYSGuideViewController alloc] init];
        // 如果没有，表示没有进入过主页，创建scrollView
        self.guideView  = [[AYSGuideView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.guideView.delegate = self;
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(SCREEN_WIDTH * 2 + 120, SCREEN_HEIGHT - 100, 100, 30);
        button.backgroundColor = [UIColor blackColor];
        [button setTitle:@"立即体验" forState:(UIControlStateNormal)];
        
        [button addTarget:self action:@selector(goToMainView) forControlEvents:(UIControlEventTouchUpInside)];
        [_guideView addSubview:button];
        [_guideVC.view addSubview:self.guideView];
        
        self.window.rootViewController = _guideVC;
    }
    
//    // 注册信鸽推送服务
//    [XGPush startApp:2200162048 appKey:@"I4Q751HM8QNX"];
//    
//    // 判断设备版本设置推送
//    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0)
//    {
//        // 注册我们推送的生意、角标、提醒
//        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
//        
//        [application registerUserNotificationSettings:settings];
//        [self createLocalNotification];
//    }
//    else
//    {
//        // 如果是8.0以下，用之前的方法去注册
//        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
//    }
    [self.window makeKeyAndVisible];
    
    return YES;
}

// 创建一个本地推送
- (void)createLocalNotification
{
    // 创建一个本地推送
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    // 设置每天10点推送消息
    NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
    if (notification != nil)
    {
        // 设置推送时间
        notification.fireDate = pushDate;
        
        // 设置时区
        notification.timeZone = [NSTimeZone defaultTimeZone];
        
        // 设置重复间隔
        notification.repeatInterval = kCFCalendarUnitDay;
        
        // 推送声音
        notification.soundName = UILocalNotificationDefaultSoundName;
        
        // 推送内容
        notification.alertBody = @"爱影视有更新啦，快进来看看吧！";
        
        // 显示程序右上角的通知数量
        notification.applicationIconBadgeNumber = 1;
        
        //设置userinfo 方便再之后需要撤销的时候使用
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
        notification.userInfo = userInfo;
        
        // 添加推送到UIApplication
        UIApplication *app = [UIApplication sharedApplication];
        [app scheduleLocalNotification:notification];
    }
}

// 点击提示框的打开
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    application.applicationIconBadgeNumber = 0;
}

// 当程序还在后台运行
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    application.applicationIconBadgeNumber = 0;
}


// 执行代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = self.guideView.contentOffset.x/SCREEN_WIDTH;
    if (self.guideView.contentOffset.x > SCREEN_WIDTH * 4) {
        self.isOut = YES;
    }
}
// 停止滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.isOut == YES)
    {
          [self.guideView removeFromSuperview];
          [self goToMainView];
    }
}
- (void)goToMainView
{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *file = [path stringByAppendingString:@"aa.txt"];
    
    // 判断是否创建了沙盒里的文件
    if (![manager fileExistsAtPath:file])
    {
        // 如果没有就创建文件
        [manager createFileAtPath:file contents:nil attributes:nil];
    }
     self.window.rootViewController = _sideViewController;
   
}
- (void)pushVC:(NSNotification *)notification
{
    if ([notification.userInfo[@"row"] isEqual:@"0"])
    {
        if (_isHave == NO)
        {
            AYSWatchRecordViewController *watchVC = [[AYSWatchRecordViewController alloc] init];
            [((UINavigationController *)_rootVC.selectedViewController) pushViewController:watchVC animated:YES];
            [_sideViewController hideSideViewController:YES];

        }
    }
    else if([notification.userInfo[@"row"] isEqual:@"1"])
    {
        AYSCollectAllViewController *collectVC = [[AYSCollectAllViewController alloc] init];
        [((UINavigationController *)_rootVC.selectedViewController) pushViewController:collectVC animated:YES];
        [_sideViewController hideSideViewController:YES];
    }
//    else if([notification.userInfo[@"row"] isEqual:@"2"])
//    {
//        AYSChangeSkinViewController *changeSkinVC = [[AYSChangeSkinViewController alloc] init];
//        [((UINavigationController *)_rootVC.selectedViewController) pushViewController:changeSkinVC animated:YES];
//        [_sideViewController hideSideViewController:YES];
//    }
//    else if([notification.userInfo[@"row"] isEqual:@"3"])
//    {
//        AYSSetLanguageViewController *setVC = [[AYSSetLanguageViewController alloc] init];
//        [((UINavigationController *)_rootVC.selectedViewController) pushViewController:setVC animated:YES];
//        [_sideViewController hideSideViewController:YES];
//    }
    else if([notification.userInfo[@"row"] isEqual:@"3"])
    {
        AYSDisclaimerViewController *rootVC = [[AYSDisclaimerViewController alloc] init];
        [((UINavigationController *)_rootVC.selectedViewController) pushViewController:rootVC animated:YES];
        [_sideViewController hideSideViewController:YES];
    }
    else if([notification.userInfo[@"row"] isEqual:@"4"])
    {
        AYSAboutUsViewController *aboutVC = [[AYSAboutUsViewController alloc] init];
        [((UINavigationController *)_rootVC.selectedViewController) pushViewController:aboutVC animated:YES];
        [_sideViewController hideSideViewController:YES];
    }
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}



- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
